package com.ruoyi.project.wodemand.service.impl;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.hr.domain.THrContractinfo;
import com.ruoyi.project.hr.service.impl.THrContractinfoServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.wodemand.mapper.TCondtionJudgeMapper;
import com.ruoyi.project.wodemand.domain.TCondtionJudge;
import com.ruoyi.project.wodemand.service.ITCondtionJudgeService;

/**
 * 逻辑条件Service业务层处理
 *
 * @author lt
 * @date 2020-04-21
 */
@Service
public class TCondtionJudgeServiceImpl implements ITCondtionJudgeService
{
    private static final Logger log = LoggerFactory.getLogger(TCondtionJudgeServiceImpl.class);
    @Autowired
    private TCondtionJudgeMapper tCondtionJudgeMapper;

    /**
     * 查询逻辑条件
     *
     * @param id 逻辑条件ID
     * @return 逻辑条件
     */
    @Override
    public TCondtionJudge selectTCondtionJudgeById(Long id)
    {
        return tCondtionJudgeMapper.selectTCondtionJudgeById(id);
    }

    /**
     * 查询逻辑条件列表
     *
     * @param tCondtionJudge 逻辑条件
     * @return 逻辑条件
     */
    @Override
    public List<TCondtionJudge> selectTCondtionJudgeList(TCondtionJudge tCondtionJudge)
    {
        return tCondtionJudgeMapper.selectTCondtionJudgeList(tCondtionJudge);
    }

    /**
     * 新增逻辑条件
     *
     * @param tCondtionJudge 逻辑条件
     * @return 结果
     */
    @Override
    public int insertTCondtionJudge(TCondtionJudge tCondtionJudge)
    {
        return tCondtionJudgeMapper.insertTCondtionJudge(tCondtionJudge);
    }

    /**
     * 修改逻辑条件
     *
     * @param tCondtionJudge 逻辑条件
     * @return 结果
     */
    @Override
    public int updateTCondtionJudge(TCondtionJudge tCondtionJudge)
    {
        return tCondtionJudgeMapper.updateTCondtionJudge(tCondtionJudge);
    }

    /**
     * 批量删除逻辑条件
     *
     * @param ids 需要删除的逻辑条件ID
     * @return 结果
     */
    @Override
    public int deleteTCondtionJudgeByIds(Long[] ids)
    {
        return tCondtionJudgeMapper.deleteTCondtionJudgeByIds(ids);
    }

    /**
     * 删除逻辑条件信息
     *
     * @param id 逻辑条件ID
     * @return 结果
     */
    @Override
    public int deleteTCondtionJudgeById(Long id)
    {
        return tCondtionJudgeMapper.deleteTCondtionJudgeById(id);
    }

    /**
     * 补充的判断值
     *
     * @param userList       补充的判断值
     * @param isUpdateSupport
     * @param operName        操作用户
     * @return 结果`
     */
    @Override
    public String importJudge(List<TCondtionJudge> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new CustomException("补充的判断值不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TCondtionJudge company : userList) {
            try {
                String s1 = fieldIsNull(company);
                if (s1.equals("1001")) {
                    return failureMsg.insert(0, "工单类型ID\t工单对应条件编号\t判断值\n").toString();
                }
                company.setReserved1(operName);
                // 验证是否存在这个用户
                tCondtionJudgeMapper.insertTCondtionJudge(company);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("工单类型ID").append(company.getSheetTypeId()).append(" 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、工单类型ID " + company.getCondValueList()+ " 插入失败";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


    private String fieldIsNull(TCondtionJudge company) {
        String code = "";
        if (StringUtils.isEmpty(company.getCondValueList())
                || StringUtils.isEmpty(company.getSheetTypeId())
                || StringUtils.isEmpty(company.getCondId().toString())
        )
        {
            code = "1001";
        }
        return code;
    }
}
